package io.renren.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.dao.MarketmxDao;
import io.renren.dto.FoodDTO;
import io.renren.dto.MarketmxDTO;
import io.renren.dto.SupplierDTO;
import io.renren.entity.FoodEntity;
import io.renren.entity.MarketmxEntity;
import io.renren.service.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 市场监管明细表
 *
 * @author 罗胜平 781757616@qq.com
 * @since 1.0.0 2019-11-22
 */
@Service
public class MarketmxServiceImpl extends CrudServiceImpl<MarketmxDao, MarketmxEntity, MarketmxDTO> implements MarketmxService {

    @Autowired
    private FoodService foodService;
    @Autowired
    private SupplierService supplierService;

    @Autowired
    private MarketmxDao marketmxDao;
    @Override
    public QueryWrapper<MarketmxEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");

        QueryWrapper<MarketmxEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }

    @Override
    public void savaUnitPrice(MarketmxDTO dto){
        FoodDTO foodDTO=foodService.get(dto.getId());
        dto.setFoodId(foodDTO.getFoodId());
        dto.setFoodName(foodDTO.getFoodName());
        dto.setUnit(dto.getUnit());
        dto.setOperatorTime(new Date());
        save(dto);
    }

    @Override
    public List<MarketmxDTO> selectDetails(long id) {
        return marketmxDao.selectDetail(id);
    }

    @Override
    public List<MarketmxDTO> slectPages() {
        return marketmxDao.slectPage();
    }

    /**
     * 市场价格对比
     * @param dto
     * @return
     */
    @Override
    public Map slectCompare(MarketmxDTO dto) {
        String bgeinDate = dto.getBgeinDate();
        String endDate = dto.getEndDate();
        float unitPrice = 0F;
        float compareResult ;
        if(null != dto.getUnit()){
            unitPrice = dto.getUnitPrice();
        }
        List<Map> list=new ArrayList<>();
        //查询食材是主材还是辅材
        FoodDTO foodDTO=foodService.get(dto.getFoodId());
        Map map = new HashMap();
        if(1==foodDTO.getType()){
            //开始时间和结束时间查询 单个产品对应点所有供应商
            List<MarketmxDTO> supplierDtoList = marketmxDao.slectSupplierOderUnit(dto.getFoodId(), bgeinDate, endDate);
            if(supplierDtoList.size()>0){
                map.put("totalNum",supplierDtoList.get(0).getTotalNum());
                //开始时间和结束时间查询  查询出每个供应商的产品价格
                HashMap hashMap = new HashMap();
                for (MarketmxDTO marketmxDTO: supplierDtoList) {
                    hashMap.put("supplier",marketmxDTO.getSupplierId());
                    SupplierDTO supplierName = supplierService.getSupplier(marketmxDTO.getSupplierId());
                    hashMap.put("supplierName",supplierName.getSupplierName());
                    //供应商时间内的商品报价
                    List<MarketmxDTO> suppliermxList = marketmxDao.slectSupplierOdermxUnit(dto.getFoodId(),marketmxDTO.getSupplierId(), bgeinDate, endDate);
                    //时间内的市场检查价格和供应商的报价作对比
                    for (MarketmxDTO marketmx:suppliermxList ) {
                        Float supplierUnitPrice = marketmx.getSupplierUnitPrice();
                        compareResult = supplierUnitPrice-unitPrice;
                        marketmx.setCompareResult(compareResult);
                        marketmx.setUnit(foodDTO.getUnit());
                        marketmx.setFoodName(foodDTO.getFoodName());
                    }
                    hashMap.put("list",suppliermxList);
                    list.add(hashMap);
                }
            }
        }else {
            List<MarketmxDTO> marketmxDTOS = marketmxDao.slectSupplierAuxUnit(dto.getFoodId(), bgeinDate, endDate);
            if(marketmxDTOS.size()>0){
                map.put("totalNum",marketmxDTOS.get(0).getTotalNum());
                //开始时间和结束时间查询  查询出每个供应商的产品价格
                HashMap hashMap = new HashMap();
                for (MarketmxDTO marketmxDTO: marketmxDTOS) {
                    hashMap.put("supplier",marketmxDTO.getSupplierId());
                    SupplierDTO supplierName = supplierService.getSupplier(marketmxDTO.getSupplierId());
                    hashMap.put("supplierName",supplierName.getSupplierName());
                    //供应商时间内的商品报价
                    List<MarketmxDTO> suppliermxList = marketmxDao.slectSupplierAuxmxUnit(dto.getFoodId(),marketmxDTO.getSupplierId(), bgeinDate, endDate);
                    //时间内的市场检查价格和供应商的报价作对比
                    for (MarketmxDTO marketmx:suppliermxList ) {
                        Float supplierUnitPrice = marketmx.getSupplierUnitPrice();
                        compareResult = supplierUnitPrice-unitPrice;
                        marketmx.setCompareResult(compareResult);
                        marketmx.setUnit(foodDTO.getUnit());
                        marketmx.setFoodName(foodDTO.getFoodName());
                    }
                    hashMap.put("list",suppliermxList);
                    list.add(hashMap);
                }
            }
        }
        map.put("list",list);
        return map;
    }

}